package com.yaozheng.vocationaltraining.activity;

import android.content.Intent;
import android.support.annotation.IdRes;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.adapter.HundredQuestionsEncyclopediaAdapter;
import com.yaozheng.vocationaltraining.adapter.MeQuestionsEncyclopediaAdapter;
import com.yaozheng.vocationaltraining.bean.CategoriesBean;
import com.yaozheng.vocationaltraining.bean.HundredQuestionsEncyclopediaItemBean;
import com.yaozheng.vocationaltraining.iview.IHundredQuestionsEncyclopediaView;
import com.yaozheng.vocationaltraining.iview.ILoadMoreView;
import com.yaozheng.vocationaltraining.iview.IMeQuestionsEncyclopediaView;
import com.yaozheng.vocationaltraining.manage.ListViewLoadMoreManage;
import com.yaozheng.vocationaltraining.service.HundredQuestionsEncyclopediaService;
import com.yaozheng.vocationaltraining.service.MeQuestionsEncyclopediaService;
import com.yaozheng.vocationaltraining.service.impl.HundredQuestionsEncyclopediaServiceImpl;
import com.yaozheng.vocationaltraining.service.impl.MeQuestionsEncyclopediaServiceImpl;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by moon on 2017/11/9.
 */

/**
 * 我的百问百答页面
 */
@EActivity(R.layout.activity_me_questions_encyclopedia)
public class MeQuestionsEncyclopediaActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, ILoadMoreView,
        IMeQuestionsEncyclopediaView {

    @ViewById
    ListView listView;

    @ViewById
    RadioGroup radiogroup;
    @ViewById
    RadioButton my_question_radiobutton;
    @ViewById
    RadioButton my_encyclopedia_radiobutton;



    /**
     * 下拉刷新
     */
    @ViewById
    SwipeRefreshLayout swipeRefreshLayout;

    ListViewLoadMoreManage listViewLoadMoreManage;

    @Bean(MeQuestionsEncyclopediaServiceImpl.class)
    MeQuestionsEncyclopediaService meQuestionsEncyclopediaService;

    MeQuestionsEncyclopediaAdapter meQuestionsEncyclopediaAdapter;
    ArrayList<HundredQuestionsEncyclopediaItemBean> questionsEncyclopediaItemData = new ArrayList<>();


    int uid;

    int lastId;
    LayoutInflater inflater;

    @AfterViews
    public void initView() {
        setHeadTitle("百问百答");
        inflater = this.getLayoutInflater();

        uid = this.getIntentInt("uid");

        meQuestionsEncyclopediaService.init(this);
        listViewLoadMoreManage = new ListViewLoadMoreManage(this, this,
                listView);
        swipeRefreshLayout.setOnRefreshListener(this);
//		swipeRefreshLayout.postDelayed(new Runnable() {
//			@Override
//			public void run() {
//				swipeRefreshLayout.setRefreshing(true);
//
//			}
//		}, 500);

        radiogroup.check(R.id.my_question_radiobutton);

        radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                onRefresh();
            }
        });


        meQuestionsEncyclopediaAdapter = new MeQuestionsEncyclopediaAdapter(this,
                questionsEncyclopediaItemData);
        listView.setAdapter(meQuestionsEncyclopediaAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent();
                intent.putExtra("uid",uid);
                intent.putExtra("questionId",questionsEncyclopediaItemData.get(i).getId());
                intent.setClassName(MeQuestionsEncyclopediaActivity.this,
                        Constants.QUESTION_DETAIL_ACTIVITY_CLASS_NAME);
                MeQuestionsEncyclopediaActivity.this.startActivity(intent);
            }
        });

        onRefresh();


    }


    @Override
    public void onRefresh() {
        lastId = 0;
        loadData();
    }

    @Override
    public void loadMoreData() {
        if(questionsEncyclopediaItemData != null && questionsEncyclopediaItemData.size() > 0){
            int lastId = questionsEncyclopediaItemData.get(questionsEncyclopediaItemData.size() -1).getId();
            if (lastId > 0) {
                this.lastId = lastId;
                loadData();
            }
        }
    }
    public void loadData() {
        int type =1 ;//1表示我的提问 ，2表示我的回答
        switch (radiogroup.getCheckedRadioButtonId()){
            case R.id.my_question_radiobutton:
                type = 1;
                break;
            case R.id.my_encyclopedia_radiobutton:
                type = 2;
                break;
        }

        meQuestionsEncyclopediaService.list(type,lastId);
    }

    @UiThread
    @Override
    public void responseSuccess(JSONObject jsonObject) {
        try {
            cancelLoadDataProgressDialog();
            JSONArray dataList = TypeUtils.getJsonArray(jsonObject, "data","questions");
            if (dataList != null) {
                if (lastId == 0) {
                    questionsEncyclopediaItemData.clear();
                }

                for (int i = 0; i < dataList.size(); i++) {
                    JSONObject jsonObject2 = dataList.getJSONObject(i);

                    HundredQuestionsEncyclopediaItemBean bean = new HundredQuestionsEncyclopediaItemBean();
                    bean.setId(jsonObject2.getInt("id"));
                    bean.setUid(jsonObject2.getInt("uid"));
                    bean.setCateId(jsonObject2.getInt("cateId"));
                    bean.setCateName(jsonObject2.getString("cateName"));
                    bean.setTitle(jsonObject2.getString("title"));
                    bean.setAnswer(jsonObject2.getInt("answer"));
                    bean.setScore(jsonObject2.getInt("score"));
                    bean.setLike(jsonObject2.getInt("like"));
                    bean.setClick(jsonObject2.getInt("click"));
                    bean.setCreatedTime(jsonObject2.getString("createdTime"));
                    if(jsonObject2.containsKey("resolved")){
                        bean.setResolved(jsonObject2.getInt("resolved"));
                    }
                    if(jsonObject2.containsKey("selected")){
                        bean.setSelected(jsonObject2.getInt("selected"));
                    }

                    questionsEncyclopediaItemData.add(bean);
                }

                meQuestionsEncyclopediaAdapter.setData(questionsEncyclopediaItemData);
                meQuestionsEncyclopediaAdapter.notifyDataSetChanged();
            }
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
        }
    }

    @UiThread
    @Override
    public void responseError(String error) {

    }

    @Override
    public void cancelLoadDataProgressDialog() {
        swipeRefreshLayout.setRefreshing(false);
        listViewLoadMoreManage.loadMoreComplete();
    }

}
